<?php
namespace Quality\Start\Infra\DependencyInjection;

use \Doctrine\ORM\EntityManager;
use \Doctrine\Common\Cache\ApcCache;
use \Doctrine\Common\Cache\ArrayCache;
use \Doctrine\ORM\Configuration;

class Container extends \Symfony\Component\DependencyInjection\Container
{
    /**
     * @var \Quality\Start\Application
     */
    protected $application;

    /**
     * @param \Quality\Start\Application $application
     */
    public function setApplication(Application $application)
    {
        $this->application = $application;
    }

    /**
     * Gets the 'entityManager' service.
     *
     * @return \Symfony\Component\HttpFoundation\Session\Session
     */
    protected function getEntityManagerService()
    {
        return $this->services['entityManager'] = EntityManager::create(
            array(
                'host' => $this->getParameter('db.host'),
                'dbname' => $this->getParameter('db.schema'),
                'user' => $this->getParameter('db.user'),
                'password' => $this->getParameter('db.password'),
                'driver' => $this->getParameter('db.driver'),
                'charset' => $this->getParameter('db.charset')
            ),
            $this->get('doctrine.config')
        );
    }

    /**
     * @return \Doctrine\ORM\Configuration
     */
    protected function getDoctrine_ConfigService()
    {
        $this->services['doctrine.config'] = $instance = new Configuration();

        $baseDir = realpath(__DIR__ . '/../../../../../') . '/';
        $cache = $this->getParameter('doctrine.cache') == 'apc' ? new ApcCache() : new ArrayCache();
        $instance->setMetadataCacheImpl($cache);
        $instance->setQueryCacheImpl($cache);
        $instance->setResultCacheImpl($cache);
        $instance->setProxyDir($baseDir . $this->getParameter('doctrine.proxy.dir'));
        $instance->setProxyNamespace($this->getParameter('doctrine.proxy.namespace'));
        $instance->setAutoGenerateProxyClasses($this->getParameter('doctrine.proxy.auto'));
        $instance->setMetadataDriverImpl(
            $instance->newDefaultAnnotationDriver(
                $baseDir . $this->getParameter('doctrine.entity.dir')
            )
        );

        return $instance;
    }
}
